import java.io.*;
import java.util.Arrays;
import java.util.Collections;

/**
 * @Author: zhuchunfeng
 * @Description
 * @Date: 2020/1/30 10:53
 * Copyright (c) 2019 北京新媒传信科技有限公司
 */
public class Bill {

    public static Double 支出 = 0d;
    public static Double 收入 = 0d;

    public static final String WEI_XIN = "C:\\Users\\zhu.chunfeng\\Desktop\\project\\gitee\\demo-parent\\5easy-excel\\src\\main\\resources\\weixin";

    public static void main(String[] args) {
        File file = new File(WEI_XIN);
        File[] files = file.listFiles();
        Arrays.stream(files).forEach(
                f ->{
                    File[] files1 = f.listFiles();
                    if (files1.length != 1) {
                        throw new RuntimeException("exception1");
                    }
                    File wei = files1[0];
                    try {
                        test(wei);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
        );

        System.out.println("收入" + 收入);
        System.out.println("支出" + 支出);
    }

    private static void handlefile(File wei) throws Exception  {
        System.out.println(wei.getName());
        FileReader reader = new FileReader(wei);

    }

    public static void test(File wei) {
        try {
            File fileWriter = new File("C:\\Users\\zhu.chunfeng\\Desktop\\project\\gitee\\demo-parent\\5easy-excel\\src\\main\\java\\微信支付账单(20170701-20200130)_.csv");
            FileWriter fileWriter1 = new FileWriter(fileWriter, true);
            //先FileReader把文件读出来再bufferReader按行读  reader.readLine(); 没有标题用不着了
            BufferedReader reader = new BufferedReader(new FileReader(wei));
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] item = line.split(",");//一行数组
                if (item.length > 3 && item[2].contains("杰培")) {

                    item[5] = item[5].substring(1);
                    System.out.println(Arrays.asList(item));
                    //
                    fileWriter1.write(String.join(",",item) + "\n");

                    String s = item[5];
                    Double d = Double.valueOf(s);
                    if (item[4].equalsIgnoreCase("支出")) {
                        支出 += d;
                    }else if (item[4].equalsIgnoreCase("收入")) {
                        收入 += d;
                    }
                }

            }
            fileWriter1.flush();
            fileWriter1.close();
        } catch (Exception e) {
            //在命令行打印异常信息在程序中出错的位置及原因。
            e.printStackTrace();
        }
    }

}
